package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryBrandMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceBrandImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand>implements CategoryBrandService {

    @Autowired
    private CategoryService categoryService;

    //根据品牌id，查询商品分类的集合
    @Override
    public List<CategoryDTO> queryCategoriesByBrandId(Long bid) {
        //根据品牌id，查询中间表
        List<CategoryBrand> brand_ids = this.query().eq("brand_id", bid).list();

        List<CategoryDTO> CategoryList = new ArrayList<>();
        //获取商品id
        for (CategoryBrand brand_id : brand_ids) {
            Long categoryId = brand_id.getCategoryId();
            //根据商品id查询商品分类的集合
            CategoryList.add(categoryService.queryCategoryById(categoryId));
        }
        return CategoryList;
    }
}
